﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Configuration;

namespace Card.Security
{
    public class CookieManager
    {
        public static string CookieDomain
        {
            get
            {
                if (ConfigurationManager.AppSettings[CONST.AppSettings.CookieDomain] != null)
                {
                    return ConfigurationManager.AppSettings[CONST.AppSettings.CookieDomain];
                }
                return string.Empty;
            }
        }

        public static void AddCookie(string strKey, string strValue)
        {
            HttpCookie cookie = new HttpCookie(strKey);
            cookie.Value = strValue;
            if (!string.IsNullOrEmpty(CookieDomain))
            {
                cookie.Domain = CookieDomain;
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static void AddCookie(string strKey, string strValue, int expiresDays)
        {
            HttpCookie cookie = new HttpCookie(strKey);
            cookie.Value = strValue;
            cookie.Expires = DateTime.Now.Add(TimeSpan.FromDays(expiresDays));
            if (!string.IsNullOrEmpty(CookieDomain))
            {
                cookie.Domain = CookieDomain;
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static string GetCookie(string strKey)
        {
            if (HttpContext.Current.Request.Cookies[strKey] != null)
            {
                return HttpContext.Current.Request.Cookies[strKey].Value;
            }
            return string.Empty;
        }

        public static void RemoveCookie(string strKey)
        {
            AddCookie(strKey, string.Empty, -1);
        }

        public static bool ExistsCookie(string strKey)
        {
            if (HttpContext.Current.Request.Cookies[strKey] != null)
            {
                return (HttpContext.Current.Request.Cookies[strKey].Value != null);
            }
            return false;
        }

        public static void AddPassportCookie(Passport passport)
        {
            AddCookie(CONST.CookieNames.Passport, passport.ToString());
        }

        public static void RemovePassport()
        {
            RemoveCookie(CONST.CookieNames.Passport);
        }

        public static bool ExistsPassport()
        {
            return CookieManager.ExistsCookie(CONST.CookieNames.Passport);
        }

        public static Passport GetPassport()
        {
            return Passport.Parse(CookieManager.GetCookie(CONST.CookieNames.Passport));
        }

        public static string GetArea()
        {
            return GetCookie(CONST.CookieNames.Area);
        }

        public static void SetArea(string city)
        {
            AddCookie(CONST.CookieNames.Area,city,360);
        }
    }
}
